module.exports = function (server) {

    // Don't expose our internal server to the outside.
    var io = require('socket.io')(server, {
        cors: {
            origin: ["https://admin.socket.io"],
            credentials: true
        }
    });

    const { instrument } = require("@socket.io/admin-ui");
    instrument(io, {
        auth: false,
        // readonly: true
        // mode: "production"
    });

    // Tell Socket.IO to use the redis adapter. By default, the redis
    // server is assumed to be on localhost:6379. You don't have to
    // specify them explicitly unless you want to change them.
    io.adapter(require('socket.io-redis')({ host: 'localhost', port: 6379 }));
    const ioe = require('socket.io-emitter')({ host: 'localhost', port: 6379 });

    io.on("connection", (socket) => {
        console.log(`connect ${socket.id}`);

        socket.join("room42");

        socket.emit("data", "hello world.");
        socket.broadcast.emit('data', 'to all clients except sender');
        socket.to('room42').emit('data', "to all clients in 'room42' room except sender");

        socket.on("data", (arg) => {
            console.log(`client data: ${arg}`);
        });

        socket.on("disconnect", (reason) => {
            console.log(`disconnect ${socket.id} due to ${reason}`);
        });

        io.emit('data', 'to all clients');
        io.to('room42').emit('data', "to all clients in 'room42' room");

        setInterval(function () {
            ioe.emit('data', new Date);
        }, 1000);

    });
}